import tools from './tools'

export default {
    tools,

    // 检查链接是否视频
    $isVideo(url) {
        return /.+\.(mp4|mov)$/i.test(url)
    },

    // 处理分享图片的格式
    $formatShareImg(url, isPoster) {
        if (!url) return ''
        if (url.indexOf('image.mycplife.com') !== -1) { // 七牛图片
            let fixUrl = url.split('?')[0].replace('http://', 'https://')
            if (this.$isVideo(fixUrl)) {
                return fixUrl + `?vframe/jpg/offset/1/w/${isPoster ? 750 : 400}`
            } else {
                return fixUrl + `?imageView2/2/w/${isPoster ? 750 : 400}/format/jpg`
            }
        }
        return url
    },

    // 传入图片宽高和质量，通过七牛参数裁剪
    $getQiniuImgUrl({
                        url,
                        width,
                        height,
                        quality
                    }) { // 获取通过裁剪的七牛图片链接
        if (url.indexOf('image.mycplife.com') === -1 || url.indexOf('?') !== -1) { // 只有自己域名的图片才裁剪,或者已经有裁剪参数的，也直接返回
            return url
        }
        let isNumber = v => /^\d+$/.test(v)
        let getSize = size => Math.floor(size * 3)
        if (isNumber(width) && isNumber(height)) {
            return url + `?imageView2/2/w/${getSize(width)}/h/${getSize(height)}/q/${quality || 100}`
        } else if (isNumber(width)) {
            return url + `?imageView2/2/w/${getSize(width)}/q/${quality || 100}`
        } else if (isNumber(height)) {
            return url + `?imageView2/2/h/${getSize(height)}/q/${quality || 100}`
        } else {
            return url + `?imageView2/2/w/750/q/100`
        }
    },

    $setH5Title(t) {
        document.title = t
        let i = document.createElement('iframe')
        i.style.display = 'none'
        i.onload = function () {
            setTimeout(function () {
                i.remove()
            }, 9)
        }
        document.body.appendChild(i)
    }
}
